package com.xbongbong.pro.enums.teammembers.enums;

import com.xbongbong.paas.enums.PaySheetTypeEnum;
import com.xbongbong.paas.enums.PaymentSheetTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import lombok.Getter;

import java.util.List;
import java.util.Objects;

/**
 * @author: 汪腾伟
 * @date: Created in 2021/12/15 15:18
 * @description： 各业务协同人表单权限枚举
 */
@Getter
public enum FormCoUserDefaultPermissionEnum {
    /**
     * 客户管理
     */
    CUSTOMER_MANAGEMENT(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 联系人
     */
    CONTACT(XbbRefTypeEnum.CONTACT.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 销售机会
     */
    SALES_OPPORTUNITY(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 报价单
     */
    QUOTATION(XbbRefTypeEnum.QUOTATION.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 合同订单
     */
    CONTRACT(XbbRefTypeEnum.CONTRACT.getCode(), CoUserPermissionEnum.READ.getPermissionValue()),

    /**
     * 退货退款
     */
    REFUND(XbbRefTypeEnum.REFUND.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 应收款
     */
    PAYMENT(XbbRefTypeEnum.PAYMENT.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 回款单
     */
    PAYMENT_SHEET(XbbRefTypeEnum.PAYMENT_SHEET.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 付款计划
     */
    PAY_PLAN(XbbRefTypeEnum.PAY_PLAN.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 付款单
     */
    PAY_SHEET(XbbRefTypeEnum.PAY_SHEET.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 供应商
     */
    SUPPLIER(XbbRefTypeEnum.SUPPLIER.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 供应商联系人
     */
    SUPPLIER_CONTACT(XbbRefTypeEnum.SUPPLIER_CONTACT.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 采购合同
     */
    PURCHASE(XbbRefTypeEnum.PURCHASE.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 采购退货单
     */
    RETURNED_PURCHASE(XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * BOM清单
     */
    BOM_BILL(XbbRefTypeEnum.BOM_BILL.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 生产单
     */
    PRODUCTION_ORDER(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 市场活动
     */
    MARKET_ACTIVITY(XbbRefTypeEnum.MARKET_ACTIVITY.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),

    /**
     * 线索
     */
    CLUE(XbbRefTypeEnum.CLUE.getCode(), CoUserPermissionEnum.EDIT.getPermissionValue()),
    /**
     * 工单v2
     */
    WORK_ORDER_V2(XbbRefTypeEnum.WORK_ORDER_V2.getCode(), CoUserPermissionEnum.READ.getPermissionValue()),
    /**
     * 回执单
     */
    RECEIPT_ORDER(XbbRefTypeEnum.RECEIPT_ORDER.getCode(), CoUserPermissionEnum.READ.getPermissionValue()),
    ;

    /**
     * 业务类型
     */
    private Integer businessType;

    /**
     * 协同人表单默认权限
     * @see CoUserPermissionEnum
     */
    private Integer defaultPermissionValue;

    FormCoUserDefaultPermissionEnum(Integer businessType, Integer defaultPermissionValue) {
        this.businessType = businessType;
        this.defaultPermissionValue = defaultPermissionValue;
    }

    /**
     * 根据业务类型获取默认权限
     *
     * @param businessType 业务类型
     * @return
     */
    public static Integer getDefaultPerByBus(Integer businessType) {
        List<Integer> allPaymentSheetBusinessTypes =  PaymentSheetTypeEnum.getAllBusinessType();
        List<Integer> allPaySheetBusinessTypes =  PaySheetTypeEnum.getAllBusinessType();
        if (allPaymentSheetBusinessTypes.contains(businessType)) {
            businessType = XbbRefTypeEnum.PAYMENT_SHEET.getCode();
        }
        if (allPaySheetBusinessTypes.contains(businessType)) {
            businessType = XbbRefTypeEnum.PAY_SHEET.getCode();
        }
        for (FormCoUserDefaultPermissionEnum formCoUserDefaultPermissionEnum : FormCoUserDefaultPermissionEnum.values()) {
            if (Objects.equals(businessType, formCoUserDefaultPermissionEnum.getBusinessType())) {
                return  formCoUserDefaultPermissionEnum.getDefaultPermissionValue();
            }
        }
        return null;
    }


    /**
     * 获取需要判断协同人可编辑的业务
     *
     * @param businessType 业务类型
     * @return
     */
    public static FormCoUserDefaultPermissionEnum getFormPatternEnumByBus(Integer businessType) {
        List<Integer> allPaymentSheetBusinessTypes =  PaymentSheetTypeEnum.getAllBusinessType();
        List<Integer> allPaySheetBusinessTypes =  PaySheetTypeEnum.getAllBusinessType();
        if (allPaymentSheetBusinessTypes.contains(businessType)) {
            businessType = XbbRefTypeEnum.PAYMENT_SHEET.getCode();
        }
        if (allPaySheetBusinessTypes.contains(businessType)) {
            businessType = XbbRefTypeEnum.PAY_SHEET.getCode();
        }
        for (FormCoUserDefaultPermissionEnum formCoUserDefaultPermissionEnum : FormCoUserDefaultPermissionEnum.values()) {
            if (Objects.equals(businessType, formCoUserDefaultPermissionEnum.getBusinessType())) {
                return formCoUserDefaultPermissionEnum;
            }
        }
        return null;
    }
}
